﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 语法验证
{
    /// <summary>
    /// 圆形控件
    /// </summary>
    public partial class CircleControl : UserControl
    {
        /// <summary>
        /// 中心颜色
        /// </summary>
        private Color centerColor = Color.White;
        /// <summary>
        /// 中心颜色
        /// </summary>
        public Color CenterColor
        {
            set
            {
                centerColor = value;
            }
            get
            {
                return centerColor;
            }
        }

        /// <summary>
        /// 边缘颜色
        /// </summary>
        private Color edgeColor = Color.Yellow;
        /// <summary>
        /// 边缘颜色
        /// </summary>
        public Color EdgeColor
        {
            get
            {
                return edgeColor;
            }
            set
            {
                edgeColor = value;
            }
        }

        /// <summary>
        /// 中心点
        /// </summary>
        private PointF centerPoint = new PointF(3.5f, 3.5f);

        /// <summary>
        /// 中心点横坐标
        /// </summary>
        public float CenterX
        {
            set
            {
                centerPoint.X = value;
            }
            get
            {
                return centerPoint.X;
            }
        }

        /// <summary>
        /// 中心点纵坐标
        /// </summary>
        public float CenterY
        {
            set
            {
                centerPoint.Y = value;
            }
            get
            {
                return centerPoint.Y;
            }
        }

        /// <summary>
        /// 布局更改事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLayout(LayoutEventArgs e)
        {
            base.OnLayout(e);

            //绘图路径
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(new RectangleF(0f, 0f, ClientSize.Width * 1.0f, ClientSize.Height * 1.0f));
            PathGradientBrush brush = new PathGradientBrush(path);
            brush.CenterColor = centerColor;
            brush.SurroundColors = new Color[] { edgeColor };
            brush.WrapMode = WrapMode.TileFlipXY;
            brush.CenterPoint = centerPoint;

            Bitmap bitmap = new Bitmap(ClientSize.Width, ClientSize.Height);
            Graphics g = Graphics.FromImage(bitmap);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.FillPath(brush, path);
            g.DrawArc(new Pen(BackColor, 4), new RectangleF(0, 0, ClientSize.Width, ClientSize.Height), 0, 360);
            BackgroundImage = bitmap;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CircleControl()
        {
            DoubleBuffered = true;
            InitializeComponent();
        }
    }
}
